<template>
  <div>
    <div class="box">
      <el-card class="box-card" >
        <div class="post" v-for="(item,index) in articles">
          <h3 class="post-title"><a href="#" @click="jump(item)">{{ item.title }}</a></h3>
          <p class="post-meta">
            <img class="author-avatar" :src="item.imageUrl">{{ item.name }}，发布于 {{ item.createTime }}
            <el-button type="text" style="color: brown" @click="del(item, index)" v-if="isMy">删除</el-button>
          </p>
          <p class="post-text">{{ item.text }}</p>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
export default {
  name: "MyArticles",
  props: ['verifyUser'], //passvalue是父组件传递给子组件的参数
  data() {
    return {
      articles: [],
      userId: this.verifyUser,
      isMy:true,
    }
  },
  methods: {
    verify(){
      let user = this.$sessionGet();
      if(user.id === this.userId){
        this.ismy = true
      }else{
        this.ismy = false
      }
    },
    del(item,index){
      console.log(item)
      this.$axios.get("articles/delArticle?id=" + item.id)
          .then(resp => {
            this.articles.splice(index, 1);
            this.articles.sort();
            alert("删除成功");
          })
          .catch(error => {
            console.log(error)
          })
    },
    jump(item){
      let routeUrl = this.$router.resolve({
        path: "/showArticles",
        query:{
          id:item.id,
          userId:item.userId
        }
      })
      window.open(routeUrl.href, '_blank')
    },
    getList() {
      this.$axios.get("articles/getListByUserId?userId="+ this.userId)
          .then(resp => {
            let res = resp.data;
            this.articles = res.data;
            console.log(this.articles)
          })
          .catch(error => {
            console.log(error)
          })
    }
  },
  created() {
    this.getList();
  }
}
</script>

<style scoped>
.text {
  font-size: 14px;
}
.item {
  padding: 18px 0;
}
.box-card {
  width: 1000px;
}
.box{
  display: flex;
  justify-content: center;
  height: 100%;
}
.block {
  width: 100%;
}
.left {
  float: left;
  margin-right: 10px;
}
.left img{
  height: 80px;
  width: 80px;
}
.right {
  height: 100px;
  line-height: 20px;
  overflow: hidden;
}
.content {
  margin-top: 10px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  text-align: left;
  font-size: 12px;
}
.title {
  margin-bottom: 10px;
  text-align: left;
}
.bottom{
  display: flex;
}
.bottom p{
  margin: 10px;
  font-size: 10px;
}
a {
  color: #333;
  text-decoration: none;
}
section {
  background-color: #fff;
  padding: 20px;
  margin: 20px;
  text-align: left;
}
h2 {
  margin-top: 0;
  margin-bottom: 10px;
}
.tags {
  margin-top: 10px;
}
.tag {
  display: inline-block;
  margin-right: 5px;
  margin-bottom: 5px;
  padding: 5px;
  background-color: #333;
  color: #fff;
  font-size: 14px;
}
.post {
  margin-bottom: 20px;
}
.post-title {
  font-size: 18px;
  margin-bottom: 7px;
  text-align: left;
}
a:hover{
  color: cornflowerblue;
}
.post-meta {
  font-size: 14px;
  color: #999;
  display: flex;
  align-items: center;
  margin-bottom: 7px;
}
.post-text{
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  text-align: left;
  -webkit-box-orient: vertical;
}
.author-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  margin-right: 10px;
}
</style>